package com.bsoft.exchange.util;


import org.apache.commons.lang3.StringUtils;

/**
 * xml参数拼接工具
 */
public class ParamXmlUtils {


    public static String format(String parameterContent, String service) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://ws.access.hai/\">");
        stringBuilder.append("<soapenv:Header/>");
        stringBuilder.append("<soapenv:Body>");
        stringBuilder.append("<ws:invoke>");
        stringBuilder.append("<service>").append(service).append("</service>");
        stringBuilder.append("<urid>wx</urid>");
        stringBuilder.append("<pwd>wx</pwd>");
        stringBuilder.append("<parameter>");
        stringBuilder.append(parameterContent);
        stringBuilder.append("</parameter>\n" +
                "      </ws:invoke>\n" +
                "   </soapenv:Body>\n" +
                "</soapenv:Envelope>");
        return stringBuilder.toString();
    }

    /**
     * 根据身份证号码拼接参数
     *
     * @param idCard 身份证号码
     * @return 拼接好的xml参数
     */
    public static String idCardParam(String idCard) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<![CDATA[<BSXml>")
                .append("<sfzh>").append(idCard).append("</sfzh></BSXml>]]>");
        return stringBuilder.toString();
    }

    public static String codeParam(String code){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<![CDATA[<BSXml>")
                .append("<ygdm>").append(code).append("</ygdm></BSXml>]]>");
        return stringBuilder.toString();
    }

    /**
     * 检验报告列表参数
     *
     * @param patientType        患者类别
     * @param authorOrganization 就诊机构代码
     * @param clinicId           门（急）诊号
     * @return 拼接好的xml参数
     */
    public static String inspectionListParam(String patientType, String authorOrganization, String clinicId, String reportDate) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<![CDATA[<BSXml>")
                .append("<MsgHeader>\n" +
                        "         <Sender>HIS</Sender>\n" +
                        "         <MsgType>CDR_0909</MsgType>\n" +
                        "         <MsgVersion>3.0</MsgVersion>\n" +
                        "         </MsgHeader>")
                .append("<QueryCond>")
                .append("<PatientType>")
                .append(patientType)
                .append("</PatientType>")
                .append("<ClinicId>")
                .append(clinicId)
                .append("</ClinicId>")
                .append("<AuthorOrganization>").append(authorOrganization).append("</AuthorOrganization>");
        if (StringUtils.isNoneBlank(reportDate)) {
            stringBuilder.append("<ReportDate>").append(reportDate).append("</ReportDate>");
        }
        stringBuilder.append("         </QueryCond>\n" +
                "         </BSXml>\n" +
                "         ]]>");
        return stringBuilder.toString();
    }

    public static String ExamListParam(String authorOrganization, String clinicId, String reportDate){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<![CDATA[<BSXml>")
                .append("<MsgHeader>\n" +
                        "         <Sender>HIS</Sender>\n" +
                        "         <MsgType>CDR_0909</MsgType>\n" +
                        "         <MsgVersion>3.0</MsgVersion>\n" +
                        "         </MsgHeader>")
                .append("<QueryCond>")
                .append("<AuthorOrganization>").append(authorOrganization).append("</AuthorOrganization>")
                .append("<ClinicId>").append(clinicId).append("</ClinicId>");
        if (StringUtils.isNoneBlank(reportDate)) {
            stringBuilder.append("<ReportDate>").append(reportDate).append("</ReportDate>");
        }
        stringBuilder.append("</QueryCond>\n" +
                "</BSXml>\n" +
                "]]>");
        return stringBuilder.toString();
    }

    /**
     * 检验报告参数
     *
     * @param sampleNo           检验标本号
     * @param barcodeNo          条码号
     * @param authorOrganization 就诊机构代码
     * @return 拼接好的xml参数
     */
    public static String regularReportParam(String sampleNo, String barcodeNo, String authorOrganization) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<![CDATA[<BSXml>")
                .append("<MsgHeader>\n" +
                        "         <Sender>HIS</Sender>\n" +
                        "         <MsgType>CDR_0909</MsgType>\n" +
                        "         <MsgVersion>3.0</MsgVersion>\n" +
                        "         </MsgHeader>")
                .append("<QueryCond>")
                .append("<BarcodeNo>").append(barcodeNo).append("</BarcodeNo>")
                .append("<AuthorOrganization>").append(authorOrganization).append("</AuthorOrganization>")
                .append("</QueryCond></BSXml>]]>");
        return stringBuilder.toString();
    }

    /**
     * 检查报告参数
     *
     * @param reportId           报告单编号
     * @param authorOrganization 就诊机构代码
     * @return 拼接好的xml参数
     */
    public static String examReportParam(String reportId, String authorOrganization) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<![CDATA[<BSXml>")
                .append("<MsgHeader>\n" +
                        "         <Sender>HIS</Sender>\n" +
                        "         <MsgType>CDR_0909</MsgType>\n" +
                        "         <MsgVersion>3.0</MsgVersion>\n" +
                        "         </MsgHeader>")
                .append("<QueryCond>")
                .append("<ReportId>").append(reportId).append("</ReportId>")
                .append("<AuthorOrganization>").append(authorOrganization).append("</AuthorOrganization>")
                .append("</QueryCond></BSXml>]]>");
        return stringBuilder.toString();
    }
}
